package com.itsc.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.itsc.entities.ProductType;
import com.itsc.service.inter.IProductTypeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

public class ProductTypeAction implements ModelDriven<ProductType> {

	private ProductType model = new ProductType();
	private IProductTypeService productTypeService;
	
	private List<Integer> check_id;
	
	public List<Integer> getCheck_id() {
		return check_id;
	}

	public void setCheck_id(List<Integer> check_id) {
		this.check_id = check_id;
	}

	public void setProductTypeService(IProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	@Override
	public ProductType getModel() {
		// TODO Auto-generated method stub
		return model;
	}

	public String list() throws Exception {
		List<ProductType> protypeList = productTypeService.selectAll();
		ActionContext.getContext().put("protypeList", protypeList);

		return "list";
	}

	public String addModel() throws Exception {

		List<ProductType> proTypeList = productTypeService.selectByNull();
		ActionContext.getContext().put("proTypeList", proTypeList);

		return "insert";
	}

	public String checkProtype() throws Exception {
		Boolean flag = productTypeService.selectByString(model.getPt_name());

		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {

			ServletActionContext.getResponse().getWriter().print("0");
		}

		return null;
	}

	public String checkProtypeEdit() throws Exception {
		Boolean flag = productTypeService.selectByString(model.getPt_name());
		Integer id = productTypeService.selectIdByString(model.getPt_name());
		
		if (flag) {
			ServletActionContext.getResponse().getWriter().print("1");
		} else {
			if(id==model.getPt_id()){
				ServletActionContext.getResponse().getWriter().print("1");
			}else{
				ServletActionContext.getResponse().getWriter().print("0");
			}
		}

		return null;
	}

	public String add() throws Exception {

		productTypeService.save(model);
		return "ok";
	}

	public String delete() throws Exception {
		ProductType pt = productTypeService.selectById(model.getPt_id());
		productTypeService.delete(pt);
		return "ok";
	}
	
	public String edit() throws Exception{
		ProductType proTypeList = productTypeService.selectById(model.getPt_id());
		ActionContext.getContext().getValueStack().push(proTypeList);
		
		List<ProductType> ptList = productTypeService.selectByNull();
		ActionContext.getContext().put("ptList", ptList);
		
		return "edit";
	}
	
	public String update() throws Exception{
		
		productTypeService.update(model);
		return "ok";
	}
	
	public String moredelete() throws Exception{
		for(Integer id : check_id){
			ProductType pt = productTypeService.selectByIntId(id);
			if(pt!=null){
				productTypeService.delete(pt);
			}
		}
		return "ok";
	}
}
